#
# "@(#) $Id: VolumeWidget.py,v 1.1.1.1 2006-07-27 03:16:08 gioshe Exp $"
#
# This work is released under the GNU GPL, version 2 or later.
#
from qt import *
from Player import *
from Utils.Paths import *
from Utils.Settings import *

_offset_owner = None
_offset = 0

class VolumeSlider(QSlider):
	def wheelEvent(self,e):
		global _offset_owner, _offset
		if ( e.orientation() != self.orientation() and  not self.rect().contains(e.pos()) ):
			return

		if (_offset_owner != self):
			_offset_owner = self
			_offset = 0
		_offset = _offset-e.delta()*max(self.pageStep(),self.lineStep())/120
		if (abs(_offset)<1):
			return
		self.setValue( self.value() - int(_offset) )
		_offset = _offset-int(_offset)
		e.accept()

class VolumeWidget(QWidget):
	def __init__(self,container = None):
		QWidget.__init__(self,container)
		self.buildControls()
		self.setFixedWidth(70)
		volume = Settings.settings().get('Volume',50)
		self.volumeSlider.setValue(volume)
		backColor=QColor(Settings.settings().get('ColorR'),Settings.settings().get('ColorG'),Settings.settings().get('ColorB'))
		self.setPaletteBackgroundColor(backColor)
	
	def buildControls(self):
		hbox = QHBoxLayout(self)
		#hbox.setSpacing(4)
		#hbox.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
		#label = QLabel("",self)
		#label.setPixmap(QPixmap(art("labelvolumelow.png")))
		#hbox.addWidget(label)
		self.volumeSlider = VolumeSlider(Qt.Horizontal,self)
		self.volumeSlider.setFixedWidth(60)
		self.setFixedHeight(45)
		self.volumeSlider.setRange(0,100)
		QObject.connect(self.volumeSlider,SIGNAL("valueChanged(int)"),self.volumeChanged)
		hbox.addWidget(self.volumeSlider)
		#label = QLabel("",self)
		#label.setPixmap(QPixmap(art("labelvolumehigh.png")))
		#hbox.addWidget(label)
		#hbox.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
		QObject.connect(Player.singleton(),PYSIGNAL("volumeChanged"),self.gotVolumeChange)

	def volumeChanged(self,volume):
		Player.singleton().setVolume(volume,self)
	
	def gotVolumeChange(self,volume,fromObject):
		if fromObject!=self:
			self.volumeSlider.blockSignals(True)
			self.volumeSlider.setValue(volume)
			self.volumeSlider.blockSignals(False)
	
